package util.models;

import java.util.Hashtable;

public class LanguageModel {

	public Hashtable<String, Hashtable<String, Hashtable<String, Integer>>> trigrams = new Hashtable<String, Hashtable<String, Hashtable<String, Integer>>>();
	public Hashtable<String, Hashtable<String, Integer>> bigrams = new Hashtable<String, Hashtable<String, Integer>>();
	public Hashtable<String, Integer> unigrams = new Hashtable<String, Integer>();
	public Hashtable<String, Float> seeds = new Hashtable<String, Float>();

	/**
	 * Global variables
	 * 
	 * 
	 */

	public void addTrigram(String a, String b, String c, int freq_t) {
		Hashtable<String, Hashtable<String, Integer>> temporal_bi = null;
		//System.out.println("adding "+ "\t"+a +"\t"+"\t"+b+"\t"+c);
		if (trigrams.containsKey(a)) {

			temporal_bi = trigrams.get(a);

			if (temporal_bi.containsKey(b)) {
				Hashtable<String, Integer> temporal_uni = temporal_bi.get(b);

				temporal_uni.put(c, freq_t);
				temporal_bi.put(b, temporal_uni);
				trigrams.put(a, temporal_bi);

			} else {

				Hashtable<String, Integer> temporal_uni = new Hashtable<String, Integer>();
				temporal_uni.put(c, freq_t);
				temporal_bi = new Hashtable<String, Hashtable<String, Integer>>();
				temporal_bi.put(b, temporal_uni);
				trigrams.put(a, temporal_bi);
			}

		} else {
			temporal_bi = new Hashtable<String, Hashtable<String, Integer>>();
			Hashtable<String, Integer> temporal_uni = new Hashtable<String, Integer>();
			temporal_uni.put(c, freq_t);
			temporal_bi.put(b, temporal_uni);
			trigrams.put(a, temporal_bi);

		}

	}

	public void addBigram(String a, String b, int freq_b) {

		if (bigrams.containsKey(a)) {

			Hashtable<String, Integer> temp = bigrams.get(a);
			temp.put(b, freq_b);

			bigrams.put(a, temp);
		} else {

			Hashtable<String, Integer> temp = new Hashtable<String, Integer>();
			temp.put(b, freq_b);
			bigrams.put(a, temp);
			
		}

	}

	public int getBigramFreq(String a, String b) {

		if (bigrams.containsKey(a)) {

			if (bigrams.containsKey(b)) {

				return bigrams.get(a).get(b);
			}
		}
		
		if (bigrams.containsKey(b)) {

			if (bigrams.containsKey(a)) {

				return bigrams.get(b).get(a);
			}
		}
		

		return 0;

	}

	public int getUnigramFreq(String a) {

		if (unigrams.containsKey(a)) {

			return unigrams.get(a);
		}
		return  0;

	}

	public int getTrigramFreq(String a, String b, String c) {

		if (trigrams.containsKey(a)) {

			if (trigrams.containsKey(b)) {
				if (trigrams.containsKey(c)) {

					return trigrams.get(a).get(b).get(c);
				}

			}

		}

		return 0;

	}

	public void addUnigram(String a, int freq_u) {
		unigrams.put(a, freq_u);

	}

}
